﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace SerialEchoV2
{
    public partial class Form1 : Form
    {

        long bytes = 0;
        long reads = 0;
        public Form1()
        {
            InitializeComponent();
            updateReadCountDelegate = new UpdateReadCountCallback(UpdateReadCount);
            updateBytesDelegate = new UpdateBytesCallback(UpdateBytes);
        }

        delegate void UpdateReadCountCallback(string message);
        UpdateReadCountCallback updateReadCountDelegate;

        private void UpdateReadCount(string message)
        {
            if (this.InvokeRequired)
                this.Invoke(updateReadCountDelegate, message);
            else
                lblReads.Text = message;
        }

        delegate void UpdateBytesCallback(string message);
        UpdateBytesCallback updateBytesDelegate;

        private void UpdateBytes(string message)
        {
            if (this.InvokeRequired)
                this.Invoke(updateBytesDelegate, message);
            else
                lblBytes.Text = message;
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string buff = serial_port.ReadExisting();
            reads++;
            bytes += buff.Length;
            if (buff != "")
                serial_port.Write(buff);

            UpdateBytes(bytes.ToString());
            UpdateReadCount(reads.ToString());
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            foreach (string port in ports)
                lbCOMPort.Items.Add(port);

            lbCOMPort.SelectedItem = "COM3";

            int[] BaudRates = {150,
                                300,
                                600,
                                1200,
                                2400,
                                4800,
                                9600,
                                19200,
                                38400,
                                57600,
                                115200,
                                230400,
                                };
            foreach (int baud in BaudRates)
                lblBaudRate.Items.Add(baud);
            lblBaudRate.SelectedIndex = 10;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            bytes = 0;
            reads = 0;
            button3_Click(null, null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (serial_port.IsOpen)
                serial_port.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (!serial_port.IsOpen)
            {
                serial_port.PortName = (string)lbCOMPort.SelectedItem;
                serial_port.BaudRate = (int)lblBaudRate.SelectedItem;
                serial_port.Parity = Parity.None;
                serial_port.DataBits = 8;
                serial_port.StopBits = StopBits.One;
                serial_port.RtsEnable = false;
                serial_port.DtrEnable = false;
                serial_port.ReadTimeout = 0xFFFF;
                serial_port.WriteTimeout = 500;
                serial_port.Open();
            }
        }
    }
}
